using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.CommonWeb;
using System.IO;

public partial class admin_webparts_Plug_ins_Files_MoveItem : System.Web.UI.UserControl
{
    public static string ROOT_PATH = "../" + Constant.UPLOAD_FILE_PATH + "Files/";
    public static string FILES_MESSAGE = "FILES_MESSAGE";
    public static string FILES_MESSAGE_TYPE = "FILES_MESSAGE_TYPE";

    public string _Message = string.Empty;
    public string _CurrentPath = "/";
    public string _CurrentItem = "";
    public int _ActionType = 1; // 1: copy dir, 2: copy file, 3: move dir, 4: move file

    protected void Page_Load(object sender, EventArgs e)
    {

        try
        {
            string action = Request.QueryString["action"];
            if ("copydir".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                _ActionType = 1;
            }
            else if ("movedir".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                _ActionType = 3;
            }
            else if ("copyfile".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                _ActionType = 2;
            }
            else if ("movefile".Equals(action, StringComparison.OrdinalIgnoreCase))
            {
                _ActionType = 4;
            }
            else
            {
                _ActionType = 0;
            }

            if (_ActionType == 1 || _ActionType == 2)
            {
                btnSave1.Text = LanguageUtility.GetLanguageText("COPY", Session);
            }
            else
            {
                btnSave1.Text = LanguageUtility.GetLanguageText("MOVE", Session);
            }

            if (Request.QueryString["file"] != null)
            {
                _CurrentPath = Request.QueryString["file"];
            }
            else
            {
                Response.Redirect("?type=page&name=files", false);
            }

            string currentServerPath = Path.Combine(Server.MapPath(ROOT_PATH), this._CurrentPath.Replace("/", "\\").Substring(1));
            if (((_ActionType == 1 || _ActionType == 3) && !Directory.Exists(currentServerPath))
                || ((_ActionType == 2 || _ActionType == 4) && !File.Exists(currentServerPath)))
            {
                Response.Redirect("?type=page&name=files", false);
                return;
            }

            string[] paths = _CurrentPath.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
            _CurrentItem = paths[paths.Length - 1];
            if (!IsPostBack)
            {
                txtName.Text = this.ParentPath;
            }
        }
        catch (Exception)
        {
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            string currentServerPath = Path.Combine(Server.MapPath(ROOT_PATH), this._CurrentPath.TrimStart('/').Replace("/", "\\"));
            string movedPath = Path.Combine(Server.MapPath(ROOT_PATH), (txtName.Text.Trim().TrimStart('/').TrimEnd('/') + "/" + _CurrentItem.TrimStart('/')).Replace("/", "\\"));

            if ((_ActionType == 1 || _ActionType == 3) && Directory.Exists(movedPath))
            {
                this._Message = LanguageUtility.GetLanguageText("DESTINATION_DIR_EXIST", Session);
                return;
            }
            else if ((_ActionType == 2 || _ActionType == 4) && File.Exists(movedPath))
            {
                this._Message = LanguageUtility.GetLanguageText("DESTINATION_FILE_EXIST", Session);
                return;
            }

            if (!currentServerPath.Equals(movedPath))
            {
                this.SetCookie(FILES_MESSAGE_TYPE, "0");
                if (_ActionType == 1)
                {
                    this.CopyDir(new DirectoryInfo(currentServerPath), new DirectoryInfo(movedPath));
                    this.SetCookie(FILES_MESSAGE, LanguageUtility.GetLanguageText("DIR_COPIED", Session));
                }
                else if (_ActionType == 2)
                {
                    File.Copy(currentServerPath, movedPath);
                    this.SetCookie(FILES_MESSAGE, LanguageUtility.GetLanguageText("FILE_COPIED", Session));
                }
                else if (_ActionType == 3)
                {
                    Directory.Move(currentServerPath, movedPath);
                    this.SetCookie(FILES_MESSAGE, LanguageUtility.GetLanguageText("DIR_MOVED", Session));
                }
                else if (_ActionType == 4)
                {
                    File.Move(currentServerPath, movedPath);
                    this.SetCookie(FILES_MESSAGE, LanguageUtility.GetLanguageText("FILE_MOVED", Session));
                }
            }
            Response.Redirect("?type=page&name=files&file=" + Server.UrlEncode(this.ParentPath));
        }
        catch (Exception ex)
        {
            this._Message = ex.Message;
        }
    }

    public void SetCookie(string name, string value)
    {
        //CookieUtility.SetCookie(name, value, DateTime.Now.AddDays(1), Request, Response);
        Session[name] = value;
    }

    public string ParentPath
    {
        get
        {
            if (_CurrentPath == "/")
            {
                return null;
            }

            string parentPath = _CurrentPath.Substring(0, _CurrentPath.Length - 1);
            parentPath = parentPath.Substring(0, parentPath.LastIndexOf("/") + 1);

            return parentPath;
        }
    }

    public void CopyDir(DirectoryInfo source, DirectoryInfo target)
    {
        if (Directory.Exists(target.FullName) == false)
        {
            Directory.CreateDirectory(target.FullName);
        }

        foreach (FileInfo fi in source.GetFiles())
        {
            fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
        }

        foreach (DirectoryInfo diSourceDir in source.GetDirectories())
        {
            DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name);
            this.CopyDir(diSourceDir, nextTargetDir);
        }
    }
}
